package com.xiaodu.acer.phoneplay.presenter;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.orhanobut.logger.Logger;
import com.xiaodu.acer.phoneplay.bean.AppInfo;
import com.xiaodu.acer.phoneplay.bean.downloadbean.DownloadMission;
import com.xiaodu.acer.phoneplay.common.Constant;
import com.xiaodu.acer.phoneplay.common.apkparset.AndroidApk;
import com.xiaodu.acer.phoneplay.common.rx.subscriber.ProgressSubcriber;
import com.xiaodu.acer.phoneplay.common.rx.subscriber.RxSchedulers;
import com.xiaodu.acer.phoneplay.presenter.contract.AppManagerContract;
import com.xiaodu.acer.phoneplay.ui.base.BasePresenter;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observable;
import library.util.ACache;
import retrofit2.Retrofit;
import zlc.season.rxdownload3.core.Mission;


public class AppManagerPresenter extends BasePresenter<AppManagerContract.Model, AppManagerContract.View> {


    @Inject
    public AppManagerPresenter(AppManagerContract.Model model, AppManagerContract.View view) {
        super(model, view);
    }

    public void getDownLoadingApps() {
        mModel.getDownloadMissions()
                .compose(RxSchedulers.<List<Mission>>io_main())
                .subscribe(new ProgressSubcriber<List<Mission>>(mContext, mView) {
                    @Override
                    public void onNext(List<Mission> missions) {
                        List<DownloadMission> downloadMissions = new ArrayList<>();
                        for (int i = 0; i < missions.size(); i++) {
                            downloadMissions.add((DownloadMission) missions.get(i));
                        }
                        mView.showDownloading(downloadRecordFilter(downloadMissions));
                    }
                });


    }

    /**
     * 过滤取出所有已安装的包放入新的list结合中
     *
     * @return 所有已安装的包
     */
    private List<DownloadMission> downloadRecordFilter(List<DownloadMission> mList) {
        List<DownloadMission> newList = new ArrayList<>();
        for (DownloadMission mission : mList) {
            //如果不是已经成功的状态
            if (mission.getStatus() != Constant.Status_Succeed) {
                newList.add(mission);
            } else {
                newList.remove(mission);
            }
        }
        return newList;
    }

    //已完成
    public void getLocalApks() {
        mModel.getLocalApks()
                .compose(RxSchedulers.<List<AndroidApk>>io_main())
                .subscribe(new ProgressSubcriber<List<AndroidApk>>(mContext, mView) {
                    @Override
                    public void onNext(List<AndroidApk> androidApks) {
                        mView.showApps(androidApks);
                    }
                });
    }

    //已安装
    public void getInstalledApps() {
        mModel.getInstalledApps()
                .compose(RxSchedulers.<List<AndroidApk>>io_main())
                .subscribe(new ProgressSubcriber<List<AndroidApk>>(mContext, mView) {
                    @Override
                    public void onNext(List<AndroidApk> androidApks) {
                        mView.showApps(androidApks);
                    }
                });
    }

    //  需要升级的app
    public void getUpdateApps() {
        String json = ACache.get(mContext).getAsString(Constant.APP_UPDATE_LIST);

        //如果取到的json串不是空的
        if (!TextUtils.isEmpty(json)) {
            //把json串转换成List集合
            Gson gson = new Gson();
            List<AppInfo> apps = gson.fromJson(json, new TypeToken<List<AppInfo>>() {
            }.getType());

            Observable.just(apps)
                    .compose(RxSchedulers.<List<AppInfo>>io_main())
                    .subscribe(new ProgressSubcriber<List<AppInfo>>(mContext, mView) {
                        @Override
                        public void onNext(List<AppInfo> appInfos) {
                            mView.showUpdateApps(appInfos);
                        }
                    });
        }

    }


    public Retrofit getRetrofit() {
        return mModel.getRetrofit();
    }


}
